package net.oschina.archx.spring.sequoiadb.manager;

import com.sequoiadb.base.Sequoiadb;
import com.sequoiadb.base.SequoiadbDatasource;
import com.sequoiadb.base.SequoiadbOption;
import com.sequoiadb.net.ConfigOptions;
import net.oschina.archx.spring.sequoiadb.SequoiaResourceManagement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 数据库资源管理器
 * <p/>
 *
 * @author Archx
 */
public class SequoiaResourceManager implements SequoiaResourceManagement {

    private final Logger log = LoggerFactory.getLogger(getClass());

    private SequoiadbDatasource datasource;

    private ConfigOptions configOptions = new ConfigOptions();

    private SequoiadbOption sequoiadbOption = new SequoiadbOption();

    private List<String> urls;

    public void init() {
        if (urls != null && urls.size() > 0)
            this.datasource = new SequoiadbDatasource(urls, "", "", configOptions, sequoiadbOption);
        else
            throw new IllegalArgumentException("至少明确一个数据库链接地址.");
    }

    /**
     * 获取链接
     *
     * @return
     * @throws InterruptedException 线程中断异常
     */
    @Override
    public Sequoiadb getConnection() throws InterruptedException {
        return datasource.getConnection();
    }


    /**
     * 释放链接
     *
     * @param conn
     */
    @Override
    public void freeConnection(Sequoiadb conn) {
        datasource.close(conn);
    }


    public SequoiadbDatasource getDatasource() {
        return datasource;
    }

    public void setDatasource(SequoiadbDatasource datasource) {
        this.datasource = datasource;
    }

    public ConfigOptions getConfigOptions() {
        return configOptions;
    }

    public void setConfigOptions(ConfigOptions configOptions) {
        this.configOptions = configOptions;
    }

    public SequoiadbOption getSequoiadbOption() {
        return sequoiadbOption;
    }

    public void setSequoiadbOption(SequoiadbOption sequoiadbOption) {
        this.sequoiadbOption = sequoiadbOption;
    }

    public List<String> getUrls() {
        return urls;
    }

    public void setUrls(List<String> urls) {
        this.urls = urls;
    }

}
